package com.andy.qhb.action;

import java.util.List;
import java.util.Map;

import org.apache.commons.collections.MapUtils;
import org.springframework.stereotype.Component;

import com.andy.qhb.dao.DaoUtils;
import com.andy.qhb.entity.UsrGroups;
import com.andy.qhb.entity.UsrMsgLog;
import com.andy.qhb.game.GameUtils;
import com.andy.qhb.game.GameUser;
import com.andy.qhb.game.Global;
import com.andy.qhb.utils.GmAction;
import com.andy.qhb.utils.SendUtil;
import com.andy.qhb.vo.HFriend;
import com.andy.qhb.websocket.MySocketSession;

/**
 * 确认申请入群
 * @author cuiwm
 *
 */
@Component(GmAction.ACTION_PREFIX+GmAction.ACTION_119)
public class Action119 implements IWebAction{

	@Override
	public void doAction(MySocketSession myWebSocket, Map<String, Object> data) {
		
		GameUser user = myWebSocket.getUser();
		Long groupId = MapUtils.getLong(data, "groupId");
		Long msgId = MapUtils.getLong(data, "msgId");
		
		UsrGroups group = Global.getUsrGroup(groupId);
		if(group == null) return;
		
		List<UsrMsgLog> logs = group.rlogs();
		UsrMsgLog msgLog = null;
		for(UsrMsgLog log:logs) {
			if(log.getId().equals(msgId)) {
				msgLog = log;
				break;
			}
		}
		
		if(msgLog == null) return;
		
		GameUser cachUsr = GameUtils.getUsrUser(msgLog.getUid());
		
		group.addMember(cachUsr.getId(), cachUsr.getNickName(),cachUsr.getIcon());
		DaoUtils.update(group);
		SendUtil.send114(myWebSocket,groupId,cachUsr.getUser());
		
		
		MySocketSession tarSession = Global.getSession(msgLog.getUid());
		if(tarSession != null) {
			GameUser tarUser = tarSession.getUser();
			if(tarUser != null) {
				tarUser.getGroups().add(new HFriend(groupId, group.getName(),null));
				SendUtil.send113(tarSession, group);
			}
		}
		
	}

	
	
	
}
